package com.hdu.hot100.stack;


import java.util.TreeMap;

public class lc739 {
    class Solution {
        public int[] dailyTemperatures(int[] temperatures)
        {
             int len = temperatures.length;
             int[] stack = new int[len];
             int[] ans = new int[len];
             int r = 0;
             for (int i = 0; i < len; i++)
             {
                 if (r == 0 || temperatures[i] <= temperatures[stack[r - 1]])
                 {
                     stack[r++] = i;
                 }
                 else
                 {
                     while (r > 0 && temperatures[i] > temperatures[stack[r - 1]])
                     {
                         int index = stack[--r];
                         int delta = i - index;
                         ans[index] = delta;
                     }
                     stack[r++] = i;
                 }
             }
             return ans;
        }
    }
}
